package com.gkzf.ai.module.crm.controller.admin.product;

import com.gkzf.ai.framework.common.pojo.CommonResult;
import com.gkzf.ai.framework.common.pojo.PageResult;
import com.gkzf.ai.framework.datapermission.core.annotation.DataPermission;
import com.gkzf.ai.module.crm.controller.admin.product.vo.product.CrmProductPageReqVO;
import com.gkzf.ai.module.crm.controller.admin.product.vo.product.CrmProductReqVO;
import com.gkzf.ai.module.crm.controller.admin.product.vo.product.CrmProductRespVO;
import com.gkzf.ai.module.crm.controller.app.product.vo.ProductDetailsRespVO;
import com.gkzf.ai.module.crm.service.product.app.CrmProductAppService;
import com.gkzf.ai.module.crm.service.product.admin.CrmProductService;
import com.gkzf.ai.module.system.api.dict.DictDataApi;
import com.gkzf.ai.module.system.api.dict.dto.DictDataRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.gkzf.ai.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - CRM 产品")
@RestController
@RequestMapping("/crm/product")
@Validated
public class CrmProductController {

    @Resource
    private CrmProductService productService;

    @Resource
    private CrmProductAppService productAppService;

    @Resource
    private DictDataApi dictDataApi;

    @PostMapping("/create")
    @Operation(summary = "创建产品")
    @PreAuthorize("@ss.hasPermission('crm:product:create')")
    public CommonResult<Long> createProduct(@RequestBody CrmProductReqVO createReqVO) {
        return success(productService.createProduct(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品")
    @PreAuthorize("@ss.hasPermission('crm:product:update')")
    public CommonResult<Boolean> updateProduct(@RequestBody CrmProductReqVO updateReqVO) {
        productService.updateProduct(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:product:delete')")
    public CommonResult<Boolean> deleteProduct(@RequestParam("id") Long id) {
        productService.deleteProduct(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:product:query')")
    public CommonResult<CrmProductRespVO > getProduct(@RequestParam("id") Long id) {
        CrmProductRespVO  product = productService.getProduct(id);
        return success(product);
    }

    @GetMapping("/getByNo")
    @Operation(summary = "获得产品")
    @Parameter(name = "no", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:product:query')")
    public CommonResult<CrmProductRespVO > getProductByNo(@RequestParam("no") String no) {
        CrmProductRespVO  product = productService.getProductByNo(no);
        return success(product);
    }


    @GetMapping("/getDetails")
    @Operation(summary = "获得产品")
    @Parameter(name = "no", description = "编号", required = true, example = "1024")
    public CommonResult<ProductDetailsRespVO> getDetails(@RequestParam("id") Long id) {
        ProductDetailsRespVO appProduct = productAppService.getAppProduct(id);
        return success(appProduct);
    }


    @GetMapping("/page")
    @Operation(summary = "获得产品分页")
    @DataPermission
    @PreAuthorize("@ss.hasPermission('crm:product:query')")
    public CommonResult<PageResult<CrmProductRespVO>> getProductPage(@Valid CrmProductPageReqVO pageVO) {
        PageResult<CrmProductRespVO> pageResult = productService.getProductPage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/cache")
    @Operation(summary = "获得产品缓存字典属性")
    @PreAuthorize("@ss.hasPermission('crm:product:cache')")
    public CommonResult<List<DictDataRespDTO>> getCacheList(@RequestParam("dictType")String dictType,
                                                            @RequestParam("type")String type,
                                                            @RequestParam("label")String label) {
        List<DictDataRespDTO> dictDataList = dictDataApi.getDictDataList(dictType, type,label);
        return success(dictDataList);
    }


}
